import { defineStore } from 'pinia';
import { getToken, getOpenid, getCurrentCity, clearAuthStorage } from './helpers';
import { localStg } from '@/utils';
interface AuthState {
  /** 用户信息 */
  nurseUser: NurseUser.nurseUser;
  /** 用户token */
  token: string;

  openid: string;

  currentCity: string;

}



export const useAuthStore = defineStore('auth-store', {
  state: (): AuthState => ({
    nurseUser: {
      id: '',
      name: '',
      phone: '',
      sex: '',
      openid: '',
      nurseUserFamilyList: [],
      nurseUserAddrList: [],
      nurseUserHomeAddrList: [],
      defaultHospital: '1',
      defaultHospitalName: '广西安好医院',
      defaultHospitalLatitude:'22',
      defaultHospitalLongitude: '42',
      mobile:'',
      idCard: '',
      face: '',
      healthCode: '',
      vuid: '',
    },
    token: getToken(),

    openid: getOpenid(),

    currentCity: getCurrentCity(),
  }),
  getters: {
    /** 是否登录 */
    isLogin: state => Boolean(state.token),
    // getNurseUser: (state) => () => state.nurseUser
  },
  actions: {
    setNurseUser(nurseUser: NurseUser.nurseUser) {
      console.log("00000-----", nurseUser)
      this.nurseUser.nurseUserHomeAddrList.length = 0
      this.nurseUser.nurseUserAddrList.length = 0
      this.nurseUser.nurseUserFamilyList.length = 0
      
      // 处理 nurseUserAddrList
      const list = nurseUser.nurseUserAddrList?.map((item) => {
        return {
          ...item,
          dft: item.isDefault,
          fullAddress: `${item.areaName}${item.address}`,
          isDefault: item.isDefault == '1' ? true : false,
          mobile: '',
          idCard: '',
          face: '',
          healthCode: '',
        }
      })
      if (list && list.length > 0) nurseUser.nurseUserAddrList = [...list]
      
      // 处理 nurseUserHomeAddrList - 确保它始终是数组 nurseHomeAddrList
      if (nurseUser.nurseHomeAddrList && Array.isArray(nurseUser.nurseHomeAddrList)) {
        const homeAddrList = nurseUser.nurseHomeAddrList.map((item) => {
          return {
            ...item,
            fullAddress: `${item.areaName || ''}${item.address || ''}`,
            dft: item.isDefault,
            isDefault: item.isDefault == 'true' ? true : false,
          }
        })
        nurseUser.nurseUserHomeAddrList = [...homeAddrList]
      } else {
        // 如果传入的数据中没有 nurseUserHomeAddrList 或者不是数组，初始化为空数组
        nurseUser.nurseUserHomeAddrList = []
      }
      
      // 确保其他数组字段也是数组
      if (!Array.isArray(nurseUser.nurseUserFamilyList)) {
        nurseUser.nurseUserFamilyList = []
      }
      if (!Array.isArray(nurseUser.nurseUserAddrList)) {
        nurseUser.nurseUserAddrList = []
      }
      
      this.nurseUser = nurseUser;
      localStg.set("nurseUser", nurseUser)
    },
    setToken(token: string) {
      this.token = token;
      localStg.set("token", token)
    },
    setOpenid(openid: string) {
      this.openid = openid;
      localStg.set("openid", openid)
    },
    setCurrentCity(currentCity: string) {
      this.currentCity = currentCity;
      localStg.set("currentCity", currentCity)
    },

    /** 重置auth状态 */
    resetAuthStore() {
      clearAuthStorage();
      this.$reset();
    }
  }
});
